<?php
session_start();

/**
 * The admin controller is for pages that can only be accessed by users with administrative privileges.
 */
class Admin extends CI_Controller
{
	private $admin_roles;
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('users');
		$this->admin_roles = array(
			'System Administrator'
		);
	}
	
	public function index()
	{
		if (!$this->isAdmin())
		{
			show_404();
			return;
		}
		$data['title'] = 'SEVA Sports - Administration';
		$data['base'] = $this->config->item('base_url');
		$data['css'] = $data['base'].$this->config->item('css');
		$data['images'] = $data['base'].$this->config->item('images');
		$data['js'] = $data['base'].$this->config->item('js');
		
		// Add links to CSS and JS files in header.
		$data['head']['css'] = array('admin/admin.css');
		$data['head']['js'] = array('jquery-1.9.1.min.js');
		
		$this->load->view('templates/header', $data);
		$this->load->view('admin/index', $data);
		$this->load->view('templates/footer', $data);
	}
	
	private function isAdmin()
	{
		return isset($_SESSION['id']) && $this->users->has_role_with_name_in($_SESSION['id'], $this->admin_roles);
	}
}
